package cn.codenest.logpublisher.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;


/**
 * @author ：Hyman
 * @date ：Created in 2020/7/16 11:04
 * @description：
 * @modified By：
 * @version: $
 */

/*
 * rabbitMQ在和springboot集成时，采用了懒加载模式，配置完成后，
 * 只有发送第一个消息时才会正常生成交换机、队列等
 */
@Configuration
public class RabbitConfig {

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate template = new RabbitTemplate(connectionFactory());
        return template;
    }

    /*发后即忘模式调用*/

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
        return container;
    }

    @Bean
    TopicExchange logTopicExchange() {
        return new TopicExchange("logTopicExchange", true, false);
    }

    @Bean
    public Queue debugQueue() {
        return new Queue("debugQueue", true, false, false);
    }

    @Bean
    public Queue infoQueue() {
        return new Queue("infoQueue", true, false, false);
    }

    @Bean
    public Queue errorQueue() {
        return new Queue("errorQueue", true, false, false);
    }

    @Bean
    Binding bindingDebugQueue() {
        return BindingBuilder.bind(debugQueue()).to(logTopicExchange()).with("debug");
    }

    @Bean
    Binding bindingInfoQueue() {
        return BindingBuilder.bind(infoQueue()).to(logTopicExchange()).with("info");
    }

    @Bean
    Binding bindingErrorQueue() {
        return BindingBuilder.bind(errorQueue()).to(logTopicExchange()).with("error");
    }

    /*远程RPC方式调用*/
    @Bean
    TopicExchange eventTopicExchange() {
        return new TopicExchange("eventTopicExchange", true, false);
    }

    @Bean
    public Queue eventQueue() {
        return new Queue("eventQueue", true, false, false);
    }

    @Bean
    Binding bindingEventQueue() {
        return BindingBuilder.bind(eventQueue()).to(eventTopicExchange()).with("event");
    }
}
